@using Blazorise.Extensions;
@typeparam TItem

<Dropdown @bind-Visible="@Column.DropdownFilterVisible">
    <DropdownToggle @onclick:stopPropagation>
        <Icon Name="@IconName.Filter" Margin="MarginIs1FromStart" Clicked="() => {}" TextColor="@(IsFiltering() ? TextColor.Primary : TextColor.Default)" />
    </DropdownToggle>
    <DropdownMenu>
        <Row Padding="@PaddingIs3" Style="cursor:default" @onclick:stopPropagation @onmouseup:stopPropagation>
            @if ( ParentDataGrid.FilterMenuTemplate is null )
            {
                var filterMethod = Column.GetFilterMethod() ?? Column.GetDataGridFilterMethodAsColumn();
                <Column ColumnSize="ColumnSizeIs12">
                    <Field>
                        <Select TValue="DataGridColumnFilterMethod" SelectedValue="@filterMethod" SelectedValueChanged="e => { Column.SetFilterMethod(e); }">
                            @if ( Column.ColumnType == DataGridColumnType.Numeric || Column.ColumnType == DataGridColumnType.Date )
                            {
                                <SelectItem TValue="DataGridColumnFilterMethod" Value="@DataGridColumnFilterMethod.Equals">@Localizer.Localize( ParentDataGrid.Localizers?.FilterMethodEqualsLocalizer, "Equals" )</SelectItem>
                                <SelectItem TValue="DataGridColumnFilterMethod" Value="@DataGridColumnFilterMethod.NotEquals">@Localizer.Localize( ParentDataGrid.Localizers?.FilterMethodNotEqualsLocalizer, "Not Equals" )</SelectItem>

                                <SelectItem TValue="DataGridColumnFilterMethod" Value="@DataGridColumnFilterMethod.GreaterThan">@Localizer.Localize( ParentDataGrid.Localizers?.FilterMethodGreaterThanLocalizer, "Greater Than" )</SelectItem>
                                <SelectItem TValue="DataGridColumnFilterMethod" Value="@DataGridColumnFilterMethod.GreaterThanOrEqual">@Localizer.Localize( ParentDataGrid.Localizers?.FilterMethodGreaterThanOrEqualLocalizer, "Greater Than or Equal" )</SelectItem>
                                <SelectItem TValue="DataGridColumnFilterMethod" Value="@DataGridColumnFilterMethod.LessThan">@Localizer.Localize( ParentDataGrid.Localizers?.FilterMethodLessThanLocalizer, "Less Than" )</SelectItem>
                                <SelectItem TValue="DataGridColumnFilterMethod" Value="@DataGridColumnFilterMethod.LessThanOrEqual">@Localizer.Localize( ParentDataGrid.Localizers?.FilterMethodLessThanOrEqualLocalizer, "Less Than or Equal" )</SelectItem>
                                <SelectItem TValue="DataGridColumnFilterMethod" Value="@DataGridColumnFilterMethod.Between">@Localizer.Localize( ParentDataGrid.Localizers?.FilterMethodBetweenLocalizer, "Between" )</SelectItem>
                            }
                            else if ( Column.ColumnType == DataGridColumnType.Check )
                            {
                                <SelectItem TValue="DataGridColumnFilterMethod" Value="@DataGridColumnFilterMethod.Equals">@Localizer.Localize( ParentDataGrid.Localizers?.FilterMethodEqualsLocalizer, "Equals" )</SelectItem>
                                <SelectItem TValue="DataGridColumnFilterMethod" Value="@DataGridColumnFilterMethod.NotEquals">@Localizer.Localize( ParentDataGrid.Localizers?.FilterMethodNotEqualsLocalizer, "Not Equals" )</SelectItem>
                            }
                            else
                            {
                                <SelectItem TValue="DataGridColumnFilterMethod" Value="@DataGridColumnFilterMethod.Contains">@Localizer.Localize( ParentDataGrid.Localizers?.FilterMethodContainsLocalizer, "Contains" )</SelectItem>
                                <SelectItem TValue="DataGridColumnFilterMethod" Value="@DataGridColumnFilterMethod.StartsWith">@Localizer.Localize( ParentDataGrid.Localizers?.FilterMethodStartsWithLocalizer, "Starts With" )</SelectItem>
                                <SelectItem TValue="DataGridColumnFilterMethod" Value="@DataGridColumnFilterMethod.EndsWith">@Localizer.Localize( ParentDataGrid.Localizers?.FilterMethodEndsWithLocalizer, "Ends With" )</SelectItem>

                                <SelectItem TValue="DataGridColumnFilterMethod" Value="@DataGridColumnFilterMethod.Equals">@Localizer.Localize( ParentDataGrid.Localizers?.FilterMethodEqualsLocalizer, "Equals" )</SelectItem>
                                <SelectItem TValue="DataGridColumnFilterMethod" Value="@DataGridColumnFilterMethod.NotEquals">@Localizer.Localize( ParentDataGrid.Localizers?.FilterMethodNotEqualsLocalizer, "Not Equals" )</SelectItem>
                            }
                        </Select>
                    </Field>
                </Column>
                <Column ColumnSize="ColumnSizeIs12">
                    <Field @key=filterMethod>
                        @if ( filterMethod == DataGridColumnFilterMethod.Between )
                        {
                            <_DataGridCellFilter Column="Column" CellFilterType="DataGridCellFilterType.RangeFrom" OnFilterChanged="@SetFilterValue1" />
                            <_DataGridCellFilter Column="Column" CellFilterType="DataGridCellFilterType.RangeTo" OnFilterChanged="@SetFilterValue2" />
                        }
                        else
                        {
                            <_DataGridCellFilter Column="Column" OnFilterChanged="@(( newValue ) => Column.Filter.SearchValue = newValue)" />
                        }
                    </Field>
                </Column>
                <Column ColumnSize="ColumnSizeIs12" Flex="FlexJustifyContentEndAlignItemsCenter" Gap="GapIs2">
                    <Button Clicked="@ParentDataGrid.FilterData" Color="Color.Primary">@Localizer.Localize( ParentDataGrid.Localizers?.FilterButtonLocalizer, "Filter" )</Button>
                    <Button Clicked="@(() => ParentDataGrid.ClearFilter( Column.Field ))" Color="Color.Light">@Localizer.Localize( ParentDataGrid.Localizers?.ClearFilterButtonLocalizer, "Clear Filter" )</Button>
                </Column>
            }
            else
            {
                @ParentDataGrid.FilterMenuTemplate( new( Column,
                        () => Column.GetFilterMethod() ?? Column.GetDataGridFilterMethodAsColumn(),
                        () => Column.Filter.SearchValue,
                        EventCallback.Factory.Create<object>( this, async ( newFilterValue ) => await ParentDataGrid.OnFilterChanged( Column, newFilterValue ) ),
                        EventCallback.Factory.Create<DataGridColumnFilterMethod?>( this, Column.SetFilterMethod ),
                        EventCallback.Factory.Create( this, ParentDataGrid.FilterData ),
                        EventCallback.Factory.Create( this, async () => await ParentDataGrid.ClearFilter( Column.Field ) ) ) )
            }
        </Row>
    </DropdownMenu>
</Dropdown>